package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.GrouponMapper;
import org.linlinjava.litemall.db.models.Groupon;
import org.linlinjava.litemall.db.service.GrouponService;
import org.linlinjava.litemall.db.util.GrouponConstant;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;

@Service
public class GrouponServiceImpl extends ServiceImpl<GrouponMapper, Groupon>
        implements GrouponService {

    /**
     * 获取用户发起的团购记录
     *
     * @param userId
     * @return
     */
    @Override
    public List<Groupon> queryMyGroupon(Long userId) {
        QueryWrapper<Groupon> query = new QueryWrapper<>();
        query.eq("user_id", userId);
        query.eq("creator_user_id", userId);
        query.eq("group_id", 0);
        query.ne("status", GrouponConstant.STATUS_NONE);
        return baseMapper.selectList(query);
    }

    /**
     * 获取用户参与的团购记录
     *
     * @param userId
     * @return
     */
    @Override
    public List<Groupon> queryMyJoinGroupon(Long userId) {
        QueryWrapper<Groupon> query = new QueryWrapper<>();
        query.eq("user_id", userId);
        query.ne("group_id", 0);
        query.ne("status", GrouponConstant.STATUS_NONE);
        return baseMapper.selectList(query);
    }

    /**
     * 根据OrderId查询团购记录
     *
     * @param orderId
     * @return
     */
    @Override
    public Groupon queryByOrderId(Long orderId) {
        QueryWrapper<Groupon> query = new QueryWrapper<>();
        query.eq("order_id", orderId);
        return baseMapper.selectOne(query);
    }

    /**
     * 获取某个团购活动参与的记录
     *
     * @param id
     * @return
     */
    @Override
    public List<Groupon> queryJoinRecord(Long id) {
        QueryWrapper<Groupon> query = new QueryWrapper<>();
        query.eq("groupon_id", id);
        query.ne("status", GrouponConstant.STATUS_NONE);
        query.orderByDesc("add_time");
        return baseMapper.selectList(query);
    }

    /**
     * 根据ID查询记录
     *
     * @param userId
     * @param id
     * @return
     */
    @Override
    public Groupon queryById(Long userId, Long id) {
        QueryWrapper<Groupon> query = new QueryWrapper<>();
        query.eq("id", id);
        query.eq("user_id", id);
        return baseMapper.selectOne(query);
    }

    /**
     * 返回某个发起的团购参与人数
     *
     * @param grouponId
     * @return
     */
    @Override
    public int countGroupon(Long grouponId) {
        QueryWrapper<Groupon> query = new QueryWrapper<>();
        query.eq("groupon_id", grouponId);
        query.ne("status", GrouponConstant.STATUS_NONE);
        return baseMapper.selectCount(query);
    }

    @Override
    public boolean hasJoin(Long userId, Long grouponId) {
        QueryWrapper<Groupon> query = new QueryWrapper<>();
        query.eq("groupon_id", grouponId);
        query.eq("user_id", userId);
        query.ne("status", GrouponConstant.STATUS_NONE);
        return baseMapper.selectCount(query) > 0;
    }

    /**
     * 查询所有发起的团购记录
     *
     * @param rulesId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public IPage<Groupon> querySelective(String rulesId, Integer pageNo, Integer pageSize) {
        Page<Groupon> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Groupon> query = new QueryWrapper<>();
        query.eq(!StringUtils.isEmpty(rulesId), "rules_id", rulesId);
        query.ne("status", GrouponConstant.STATUS_NONE);
        query.eq("groupon_id", 0);

        return baseMapper.selectPage(page, query);
    }

    @Override
    public List<Groupon> queryByRuleId(Long grouponRuleId) {
        QueryWrapper<Groupon> query = new QueryWrapper<>();
        query.eq("rules_id", grouponRuleId);
        return baseMapper.selectList(query);
    }
}
